﻿using SharpCifs.Smb;
using Steeltoe.Common.Net;
using System;
using System.IO;
using System.Net;
using System.Text;

namespace WindowsShare
{
    class Program
    {
        static void Main(string[] args)
        {
            TestFolderSteeltoe();
            Console.ReadKey();

        }

        #region steeltoe
        public static void TestReadSteeltoe()
        {

            var credential = new NetworkCredential("ks", "utp@2018", null);
            var path = @"\\192.168.40.138\Share";
            using (var networkPath = new WindowsNetworkFileShare(path, credential))
            {
                using (var file = File.Open(Path.Combine(path, "xx.txt"),FileMode.OpenOrCreate))
                {
                    var stream = new MemoryStream();
                    file.CopyTo(stream);
                    Console.WriteLine(Encoding.UTF8.GetString(stream.ToArray()));
                }
            }

        }
        public static void TestWriteSteeltoe()
        {

            var credential = new NetworkCredential("ks", "utp@2018", null);
            var path = @"\\192.168.40.138\Share";
            using (var networkPath = new WindowsNetworkFileShare(path, credential))
            {
                using (var file = File.Create(Path.Combine(path, "test.txt")))
                {
                    file.Write(Encoding.UTF8.GetBytes("Hello!"));
                    Console.WriteLine("创建成功!");
                }
            }
            
        }
        public static void TestFolderSteeltoe()
        {
            var credential = new NetworkCredential("ks", "utp@2018", null);
            var path = @"\\192.168.40.138\Share";
            using (var networkPath = new WindowsNetworkFileShare(path, credential))
            {
                var files = Directory.GetFiles(path);
                foreach(var fileName in files)
                {
                    var lastModDate = File.GetLastWriteTime(Path.Combine(path, fileName)).ToLocalTime();
                    var name = fileName;
                    var type = Directory.Exists(Path.Combine(path, fileName)) ? "dir" : "file";
                    var date = lastModDate.ToString("yyyy-MM-dd HH:mm:ss");
                    var msg = $"{name} ({type}) - LastMod: {date}";
                    Console.WriteLine(msg);
                }
            }
        }
        #endregion

        #region Cifs

        public static void TestReadFile()
        {
            var auth = new NtlmPasswordAuthentication(null, "ks", "utp@2018");
            //Get target's SmbFile.
            var file = new SmbFile("smb://192.168.40.138/Share/xx.txt", auth);

            //Get readable stream.
            var readStream = file.GetInputStream();

            //Create reading buffer.
            var memStream = new MemoryStream();

            //Get bytes.
            ((Stream)readStream).CopyTo(memStream);

            //Dispose readable stream.
            readStream.Dispose();

            Console.WriteLine(Encoding.UTF8.GetString(memStream.ToArray()));

        }
        public static void TestCreatFile()
        {
            var auth = new NtlmPasswordAuthentication(null, "ks", "utp@2018");
            //Get the SmbFile specifying the file name to be created.
            var file = new SmbFile("smb://192.168.40.138/Share/xx2.txt", auth);

            //Create file.
            file.CreateNewFile();

            //Get writable stream.
            var writeStream = file.GetOutputStream();

            //Write bytes.
            writeStream.Write(Encoding.UTF8.GetBytes("Hello!"));

            //Dispose writable stream.
            writeStream.Dispose();

            Console.WriteLine("Created!");

        }
        public static void TestFolder()
        {
            var auth = new NtlmPasswordAuthentication(null, "ks", "utp@2018");
            var folder = new SmbFile("smb://192.168.40.138/Share/", auth);
            var epocDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            foreach (SmbFile item in folder.ListFiles())
            {
                var lastModDate = epocDate.AddMilliseconds(item.LastModified())
                                            .ToLocalTime();
                var name = item.GetName();
                var type = item.IsDirectory() ? "dir" : "file";
                var date = lastModDate.ToString("yyyy-MM-dd HH:mm:ss");
                var msg = $"{name} ({type}) - LastMod: {date}";
                Console.WriteLine(msg);
            }
        }
        #endregion

    }
}
